﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BookStore
{
    class Menus
    {

        static readonly string[] level0 = { "Добавить товар в магазин", "Посмотреть товары","Записать в файл и сразу прочитать","Выход" };
        static string[] container = level0;
        static readonly string[] level1_1 = { "Книга", "Журнал", "Гаета", "Брошюра" };
        static readonly string[] level1_2 = { "Книги", "Журналы", "Гаеты", "Брошюры", "Все товары" };
        static string arrow = "  >>>";
        public static int choise;
        static bool draw = true;
        static void MenuRender(int choise)
        {
            Console.Clear();
            for (int i = 0; i < container.Length; i++)
            {
                if (i == choise) Console.Write(arrow);
                Console.WriteLine("\t" + container[i]);
            }
        }

        public static int Menu()
        {
                Console.CursorVisible = false;
                choise = 0;
                while (draw == true)
                {
                    MenuRender(choise);
                    switch (Console.ReadKey(true).Key)
                    {
                        case ConsoleKey.UpArrow:
                            if (choise > 0) choise--;
                            break;

                        case ConsoleKey.DownArrow:
                            if ((choise + 1) < container.Length) choise++;
                            break;
                        case ConsoleKey.Enter:
                            MenuChoise();
                            break;
                    }
                }
            return choise;
        }

        static void MenuChoise()
        {
            if (choise == 1 && container == level0)
            {
                container = level1_2;
            }
            else if (choise == 0 && container == level0)
            {
                container = level1_1;
            }
            else if (choise == 2 && container == level0)
            {
                draw = false;
                StoreObject.ToFile();
                StoreObject.FromFile();
                Console.WriteLine("Чтение завершено");
                Console.ReadLine();
                draw = true;
            }
            else if (choise == 3 && container == level0)
            {
                choise = 999;
            }
            else
            {

                Console.CursorVisible = true;
                if (container == level1_1)
                {
                    draw = false;
                    StoreObject.AddObj(choise);
                    Console.CursorVisible = false;
                    choise = 0;
                    draw = true;
                    container = level0;

                }
                else if (container == level1_2)
                {
                    draw = false;
                    StoreObject.PrintObj(choise);
                    container = level0;
                    Console.WriteLine("Вывод завершен. Нажмите любую клавишу для продолжения.");
                    Console.ReadLine();
                    Console.CursorVisible = false;
                    choise = 0;
                    draw = true;
                }
            }

        }
    }
}
